package com.order.controller.sys;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.order.config.Consts;
import com.order.model.OrderHead;
import com.order.model.OrderLine;

/** 
* @ClassName: OrderController 
* @Description: 订单管理控制器
* @author LinBo
* @date 2014年4月22日 上午8:29:06 
*  
*/ 
@ControllerBind(controllerKey = Consts.SYS + "order")
public class OrderController extends Controller {

	/** 
	* @Title: index 
	* @描述:    定位页面
	*/ 
	public void index(){
		render(Consts.SYS + "order.htm");
	}
	
	/** 
	* @Title: getOrderJoson 
	* @Description:    获取订单json对象
	*/ 
	public void getOrderJson(){
		String jsonStr = "{\"status\":\"\",\"account_name\":\"\",\"create_date_l\":\"\",\"create_date_r\":\"\"}";
		int page = getParaToInt("page", 0);
		int pageSize = getParaToInt("pagesize", 0);
		int orderHeadId = getParaToInt("order_head_id", 0);
		String sortName = getPara("sortname");
		String sortOrder = getPara("sortorder");
		JSONObject json = JSONObject.parseObject(getPara("where", jsonStr));
		Map<String, Object> map = null;
		map = OrderHead.dao.getOrder(page, pageSize, orderHeadId, json, sortName, sortOrder);
		renderJson(map);
	}
	
	/** 
	* @Title: getDetailByOrderHeadId 
	* @Description:    通过订单头id获取订单详情
	*/ 
	public void getDetailByOrderHeadId(){
		String orderHeadId = getPara("orderHeadId");
		String detial = OrderLine.dao.getDetialById(orderHeadId);
		renderJson(detial);
	}
	
	/** 
	* @Title: doShipment 
	* @Description:    修改订单为发货状态
	*/ 
	public void doShipment(){
		String[] ids = getPara("ids", "").split(",");
		Map<Integer, String> map = OrderHead.shipment(ids);
		String msg = "";
		for(Integer key : map.keySet()){
			msg += "[代码：" + key + "]\t" + map.get(key) ;
		}
		if(msg.length() == 0){
			msg = "修改送餐状态成功";
		}
		setAttr("msg", msg);
		renderJson();
	}
	
	/** 
	* @Title: doInvalid 
	* @Description:    设置订单无效
	*/ 
	public void doInvalid(){
		String[] orderHeadIds = getPara("orderHeadIds", "").split(",");
		Map<Integer, String> map = OrderHead.doInvalid(orderHeadIds);
		String msg = "";
		for(Integer key : map.keySet()){
			msg += map.get(key) + "[代码：" + key + "]\n";
		}
		if(msg.length() == 0){
			msg = "设置订单无效成功";
		}
		setAttr("msg", msg);
		renderJson();
	}
}
